package org.example.innovate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.innovate.common.CommonResponse;
import org.example.innovate.domain.Dish;
import org.example.innovate.domain.PostView;
import org.example.innovate.domain.User;
import org.example.innovate.mapper.DishMapper;
import org.example.innovate.mapper.UserMapper;
import org.example.innovate.service.PostViewService;
import org.example.innovate.mapper.PostViewMapper;
import org.example.innovate.util.JwtUtil;
import org.example.innovate.vo.DishOneVo;
import org.example.innovate.vo.ListVo;
import org.example.innovate.vo.ViewVo;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author Administrator
* @description 针对表【post_view】的数据库操作Service实现
* @createDate 2025-03-13 17:01:49
*/
@Service
public class PostViewServiceImpl extends ServiceImpl<PostViewMapper, PostView>
    implements PostViewService{

    @Resource
    private PostViewMapper postViewMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private DishMapper dishMapper;

    @Override
    public CommonResponse<List<ViewVo>> getAll(HttpServletRequest request) {
        String token=request.getHeader("token");
        System.out.println(token);
        JwtUtil.verifyToken(token);
        User user= JwtUtil.getCurrentUser(token);
        if(user==null)
        {
            return CommonResponse.createForError("用户不存在");
        }
        List<PostView> list=postViewMapper.selectLatestPostView(user.getUserId());
        List<ViewVo> viewVos=new ArrayList<>();
        for(PostView postView:list)
        {
            QueryWrapper<Dish> dishQueryWrapper=new QueryWrapper<>();
            dishQueryWrapper.eq("dish_id", postView.getPostId());
            Dish dish=dishMapper.selectOne(dishQueryWrapper);
            QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
            userQueryWrapper.eq("user_id",dish.getUserId());
            user=userMapper.selectOne(userQueryWrapper);
            DishOneVo dishOneVo=new DishOneVo();
            dishOneVo.setUsername(user.getUsername());
            Calendar cal = Calendar.getInstance();
            cal.setTime(dish.getCreatedAt());
            cal.add(Calendar.HOUR_OF_DAY, 8);
            Date eightHoursAgo = cal.getTime();
            dish.setCreatedAt(eightHoursAgo);
            cal.setTime(dish.getUpdatedAt());
            cal.add(Calendar.HOUR_OF_DAY, 8);
            eightHoursAgo = cal.getTime();
            dish.setUpdatedAt(eightHoursAgo);
            dishOneVo.setDish(dish);
            ViewVo viewVo=new ViewVo();
            viewVo.setDish(dishOneVo);
            viewVo.setId(postView.getId());
            viewVos.add(viewVo);
        }
        return CommonResponse.createForSuccess("查询成功",viewVos);
    }

    @Override
    public CommonResponse<Integer> delete(ListVo idVo)
    {
        int[] ids=idVo.getId();
        System.out.println(Arrays.toString(ids));
        for(int id:ids)
        {
            QueryWrapper<PostView> postViewQueryWrapper=new QueryWrapper<>();
            postViewQueryWrapper.eq("id", id);
            PostView postView=postViewMapper.selectOne(postViewQueryWrapper);
            QueryWrapper<PostView> postViewQueryWrapper2=new QueryWrapper<>();
            postViewQueryWrapper2.eq("post_id", postView.getPostId());
            postViewMapper.delete(postViewQueryWrapper2);
        }
        return CommonResponse.createForSuccess();
    }
}




